<?php

class User_m extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
    }
    
    function add_user($data){
        $this->db->set('user_name',$data['user_name']);
        $this->db->set('user_pwd',md5($data['user_pwd']));
        $this->db->set('user_role',$data['user_role']);
        $this->db->insert('user');
    }
    
    function get_user($data){
        $this->db->where('user_name',$data['user_name']);
        $this->db->where('user_pwd',md5($data['user_pwd']));
        $result = $this->db->get('user');
        return $result;
    }
    
    function get_active_user(){
        $user_id = $this->session->userdata('user_id');
        if(empty($user_id)){
            return null;
        }else{
            $this->db->where('user_id',$user_id);
            $result = $this->db->get('user');
            if($result->num_rows()>0){
                return $result->first_row();
            }else{
                return null;
            }
        }
    }
    
    function change_pass($user_id,$new){
        $this->db->set('user_pwd',md5($new));
        $this->db->where('user_id',$user_id);
        $this->db->update('user');        
    }
}